const request=require("superagent");
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const util = require('util');
const charset = require("superagent-charset")(request);
const fs=require('fs');
var mysql      = require('mysql');
const assert = require('assert');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : '123456',
  database : 'matter'
});
connection.query = util.promisify(connection.query);
async function getResourceTree(page) {
    var resource = await page._client.send('Page.getResourceTree');
    return resource.frameTree;
}

async function getResourceContent(page, url) {
    const { content, base64Encoded } = await page._client.send(
        'Page.getResourceContent',
        { frameId: String(page.mainFrame()._id), url },
    );
    assert.equal(base64Encoded, true);
    return content;
};
const Agent=[
 	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1",
    "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
    "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
    "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10",
    "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
    "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
    "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
];
const timeout={
	response:50000,
	deadline:600000,
}
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function delay(t){
    return new Promise(resolve => setTimeout(resolve, t))
}

function rnd(n, m){
    var random = Math.floor(Math.random()*(m-n+1)+n);
    return random;
}
function getRndIP(){
    var ip=[];
    for(var i=0;i<4;i++){
    ip.push(rnd(10,250));
    }
    return ip.join(".");
}
//封装请求 实现失败重试
async function requestWithRetry(url, retryCount = 3, timeout = 5000,option) {
  return new Promise((resolve, reject) => {
    let numRetries = 0;
 
    const makeRequest = () => {
      request
        .get(url)
        .set(option)
        .timeout(timeout) // 设置超时时间
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          if (error.code === 'ECONNABORTED' && numRetries < retryCount) {
            numRetries += 1;
            console.log(`Request timed out. Retrying... (Attempt ${numRetries})`);
            makeRequest(); // 重试请求
          } else {
            reject(error);
          }
        });
    };
 
    makeRequest();
  });
}
async function getList(){

	let sql=`select * from m_matter where id > 13427 AND base_code IS NULL`;
	let result=await connection.query(sql);
	for (var i = 0; i < result.length; i++) {
		var ip=getRndIP();
		var agent=Agent[rnd(0,Agent.length-1)];
		agent=Agent[0];
		var option={
			"Host":"zwfw.nmg.gov.cn",
			"User-Agent":agent,
			"Referer":'https://zwfw.nmg.gov.cn/',
			"X-Forwarded-For":ip,
		}
		//事项详情页
		//https://zwfw.nmg.gov.cn/handle_affairs_detail?item_detail_id=8a8e936689dec2a4018a2a896c241572&regionCode=150000&highlight=&record=istrue
		let url=`https://zwfw.nmg.gov.cn/handle_affairs_detail?item_detail_id=${result[i]['ItemID']}&regionCode=150000&highlight=&record=istrue`;
		console.log(result[i]['id'],result[i]['name'],url);
		await delay(500);
		let res = await requestWithRetry(url, 3, 5000,option).then(response => {
		    // console.log('Response received:', response);
		    return response;
		  }).catch(error => {
		    console.error('Error:', error);
		  });
		// let res=await request.get(url).set(option).timeout(timeout).buffer(true);
		let body=res.text;
		var $ = cheerio.load(body);
		//实施主体
		let implement_subject = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(2) td:eq(0)').text().trim();
		//实施主体编码
		let implement_subject_code = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(2) td:eq(1)').text().trim();
		if(implement_subject==false||implement_subject_code==false){
			console.error('实施主体或编码为空')
			continue;
			return;
		}
		//根据部门编码查部门表
		let unitResult = await connection.query(`select * from m_unit where code = '${implement_subject_code}'`);
		// console.log(unitResult[0]['full_name'])
		let unit_id = 0;
		//判断部门是否已存在
		if(unitResult.length>0){
			//存在
			//判断部门全称是否存在
			if(unitResult[0]['full_name']==null){
				//不存在 插入部门全称
				console.log('插入部门全称',implement_subject)
				await connection.query(`update m_unit SET full_name = '${implement_subject}' where id = '${unitResult[0]['id']}'`);
			}
			unit_id = unitResult[0]['id'];
		}else{
			//不存在 插入部门
			console.log('插入部门',implement_subject)
			let addSql="INSERT INTO m_unit SET  ?";
			let addSqlParams = {
				'area_id':result[i]['area_id'],
				'name':implement_subject,
				'code':implement_subject_code,
				'full_name':implement_subject
			};
			unit_id = await asySql(addSql,addSqlParams).then(insertId => {
				return insertId;
			}).catch(error => {
			  console.error(error); // 异步操作失败，输出错误信息
			});

		}
		if(unit_id>0){
			let intermediary_services_name = null;
			//实施主体性质
			let implement_subject_nature = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(3) td:eq(0)').text().trim();
			if(implement_subject_nature=='法定机关'){
				implement_subject_nature = 1;
			}else if(implement_subject_nature=='授权组织'){
				implement_subject_nature = 2;
			}else if(implement_subject_nature=='受委托组织'){
				implement_subject_nature = 3;
			}else{
				console.error('未找到实施主体性质：'+implement_subject_nature)
				return;
			}
			//行使层级
			let exercise_level = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(3) td:eq(1)').text().trim();
			if(exercise_level == '自治区级,盟市级,县级'){
				exercise_level = exercise_level.split(",");
			}else if(exercise_level == '省级、设区的市级、县级'){

				exercise_level = exercise_level.split("、");
			}else{
				exercise_level = exercise_level.split("^");
			}
			
			
			for (var e_i = 0; e_i < exercise_level.length; e_i++) {
				if(exercise_level[e_i]=='国家级'){
					exercise_level[e_i] = 1;
				}else if(exercise_level[e_i]=='自治区级'||exercise_level[e_i]=='省级'||exercise_level[e_i]=='省级、设区的市级'){
					exercise_level[e_i] = 2;
				}else if(exercise_level[e_i]=='盟市级'||exercise_level[e_i]=='设区的市级'||exercise_level[e_i]=='设区的市级、县级'||exercise_level[e_i]=='市级/隶属'){
					exercise_level[e_i] = 3;
				}else if(exercise_level[e_i]=='旗县（区）级'||exercise_level[e_i]=='县级'||exercise_level[e_i]=='市级/隶属、县级'||exercise_level[e_i]=='省级/直属、市级/隶属、县级'){
					exercise_level[e_i] = 4;
				}else if(exercise_level[e_i]=='镇级'||exercise_level[e_i]=='乡级'){
					exercise_level[e_i] = 5;
				}else if(exercise_level[e_i]=='村级'){
					exercise_level[e_i] = 6;
				}else if(exercise_level[e_i]=='自治区级,,,1'){
					exercise_level[e_i] = 2;
				}else if(exercise_level[e_i]=='分级管理'){
					exercise_level[e_i] = null;
				}else{
					console.error('未找到行使层级：'+exercise_level[e_i])
					return;
				}
			}
			exercise_level = exercise_level.join(",");
			let data = {};
			//行政征收 页面结构处理
			let type1 = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(1)').text().trim();
			if(type1 == '行政征收'){
				//事项状态
				let event_status = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(6) td:eq(0)').text().trim();
				if(event_status=='发布'){
					event_status = 1;
				}else if(event_status=='暂停'){
					event_status = 2;
				}else{
					console.error('未找到事项状态：'+event_status)
					return;
				}
				//是否收费
				let is_charge = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(7) td:eq(0)').text().trim();
				if(is_charge=='是'){
					is_charge = 1;
				}else if(is_charge=='否'){
					is_charge = 0;
				}else if(is_charge==''){
					is_charge = 0;
				}else{
					console.error('未找到是否收费：'+is_charge)
					return;
				}
				data = {
					'unit_id':unit_id,
					'base_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(0)').text().trim(),
					'implement_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(1)').text().trim(),
					'implement_subject':implement_subject,
					'implement_subject_code':implement_subject_code,
					'implement_subject_nature':implement_subject_nature,
					'exercise_level':exercise_level,
					'power_source':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(0)').text().trim(),
					'event_type':9,
					'collection_type':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(5) td:eq(0)').text().trim(),
					'is_reduction':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(5) td:eq(1)').text().trim(),
					'event_status':event_status,
					'event_version':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(6) td:eq(1)').text().trim(),
					'is_charge':is_charge
				}


			}else{
				//事项类型
				let event_type = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(5) td:eq(0)').text().trim();
				if(event_type=='行政许可'){
					event_type = 1;		
				}else if(event_type=='行政给付'){
					event_type = 2;
				}else if(event_type=='行政确认'){
					event_type = 3;
				}else if(event_type=='行政奖励'){
					event_type = 4;
				}else if(event_type=='行政裁决'){
					event_type = 5;
				}else if(event_type=='行政备案'){
					event_type = 6;
				}else if(event_type=='其它行政权力'||event_type=='其他行政权力'){
					event_type = 7;
				}else if(event_type=='公共服务'){
					event_type = 8;
				}else if(event_type=='??'){
					continue;
				}else{
					console.error('未找到事项类型：'+event_type)
					return;
				}
				//事项状态
				let event_status = $('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(6) td:eq(0)').text().trim();
				if(event_status=='发布'){
					event_status = 1;
				}else if(event_status=='暂停'){
					event_status = 2;
				}else{
					console.error('未找到事项状态：'+event_status)
					return;
				}
				//服务对象
				let service_object = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(0) td:eq(0)').text().trim();
				service_object = service_object.split("、");
				for (var s_i = 0; s_i < service_object.length; s_i++) {
					if(service_object[s_i] == '自然人'){
						service_object[s_i] = 1;
					}else if(service_object[s_i]=='事业法人'){
						service_object[s_i] = 2;
					}else if(service_object[s_i]=='企业法人'){
						service_object[s_i] = 3;
					}else if(service_object[s_i]=='非法人企业'){
						service_object[s_i] = 4;
					}else if(service_object[s_i]=='行政机关'){
						service_object[s_i] = 5;
					}else if(service_object[s_i]=='社会组织法人'){
						service_object[s_i] = 6;
					}else if(service_object[s_i]=='其它组织'||service_object[s_i]=='其他组织'){
						service_object[s_i] = 7;
					}else{
						console.error('未找到服务对象：'+service_object[s_i])
						return;
					}
				}

				service_object = service_object.join(",");
				//办件类型
				let processing_type = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(0) td:eq(1)').text().trim();
				if(processing_type=='承诺件'){
					processing_type = 1;
				}else if(processing_type=='即办件'){
					processing_type = 2;
				}else{
					console.error('未找到办件类型：'+processing_type)
					return;
				}
				//是否为涉企经营许可事项
				let aaa = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(1) th:eq(0)').text().trim();
				let tiao = 0;
				if(aaa=='是否为涉企经营许可事项'){
					tiao = 1;
				}


				//中介服务
				let intermediary_services = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(1+tiao)+') td:eq(1)').text().trim();
				let treq = tiao;
				if(intermediary_services == '是'&&$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(2+treq)+') th:eq(0)').text().trim()!='办理形式'){
					intermediary_services_name = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(2+tiao)+') td:eq(0)').text().trim();
					treq += 1;
				}

				//办理形式
				let application_form = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(2+treq)+') td:eq(0)').text().trim();
				application_form = application_form.split("、");
				for (var a_i = 0; a_i < application_form.length; a_i++) {
					if(application_form[a_i] == '网上办理'){
						application_form[a_i] = 1;
					}else if(application_form[a_i]=='窗口办理'){
						application_form[a_i] = 2;
					}else if(application_form[a_i]=='快递申请'){
						application_form[a_i] = 3;
					}else{
						console.error('未找到办理形式：'+application_form[a_i])
						return;
					}
				}
				application_form = application_form.join(",");
				//是否进驻政务大厅
				let is_entry = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(2+treq)+') td:eq(1)').text().trim();
				if(is_entry=='是'){
					is_entry = 1;
				}else if(is_entry=='否'){
					is_entry = 0;
				}else if(is_entry==''){
					is_entry = 1;
				}else{
					console.error('未找到是否进驻政务大厅：'+is_entry)
					continue;
					// return;
				}
				//是否网办
				let is_online_office = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(3+treq)+') td:eq(0)').text().trim();
				if(is_online_office=='是'){
					is_online_office = 1;
				}else if(is_online_office=='否'||is_online_office==''){
					is_online_office = 0;
				}else{
					console.error('未找到是否网办：'+is_online_office)
					return;
				}
				//计算机端是否对接单点登录
				let computer_is_login = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') td:eq(0)').text().trim();
				if(computer_is_login=='是'){
					computer_is_login = 1;
				}else if(computer_is_login=='否'){
					computer_is_login = 0;
				}else if(computer_is_login==''){
					computer_is_login = 0;
				}else{
					console.error('未找到计算机端是否对接单点登录：'+computer_is_login)
					return;
				}
				//移动端是否对接单点登录
				let phone_is_login = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') td:eq(0)').text().trim();
				if(phone_is_login=='是'){
					phone_is_login = 1;
				}else if(phone_is_login=='否'){
					phone_is_login = 0;
				}else if(phone_is_login==''){
					phone_is_login = 0;
				}else{
					console.error('未找到移动端是否对接单点登录：'+phone_is_login)
					return;
				}
				//是否支持自助终端办理
				let is_self_service = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(6+treq)+') td:eq(0)').text().trim();
				if(is_self_service=='是'){
					is_self_service = 1;
				}else if(is_self_service=='否'){
					is_self_service = 0;
				}else if(is_self_service==''){
					is_self_service = 0;
				}else{
					console.error('未找到是否支持自助终端办理：'+is_self_service)
					return;
				}
				//是否支持预约办理
				let is_reservation = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(6+treq)+') td:eq(1)').text().trim();
				if(is_reservation=='是'){
					is_reservation = 1;
				}else if(is_reservation=='否'){
					is_reservation = 0;
				}else{
					console.error('未找到是否支持预约办理：'+is_reservation)
					return;
				}
				//是否收费
				let is_charge = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(7+treq)+') td:eq(0)').text().trim();
				if(is_charge=='是'){
					is_charge = 1;
				}else if(is_charge=='否'||is_charge==''){
					is_charge = 0;
				}else{
					console.error('未找到是否收费：'+is_charge)
					return;
				}
				//是否支持网上支付
				let is_online_payment = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(7+treq)+') td:eq(1)').text().trim();
				if(is_online_payment=='是'||is_online_payment=='1'){
					is_online_payment = 1;
				}else if(is_online_payment=='否'||is_online_payment=='0'){
					is_online_payment = 0;
				}else if(is_online_payment==''){
					is_online_payment = 0;
				}else{
					console.error('未找到是否支持网上支付：'+is_online_payment)
					return;
				}
				//是否支持物流快递
				let is_express_delivery = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(8+treq)+') td:eq(0)').text().trim().substr(0, 1);
				if(is_express_delivery=='是'){
					is_express_delivery = 1;
				}else if(is_express_delivery=='否'){
					is_express_delivery = 0;
				}else{
					console.error('未找到是否支持物流快递：'+is_express_delivery)
					return;
				}

				
				if(tiao==0){
					data = {
							'unit_id':unit_id,
							'base_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(0)').text().trim(),
							'implement_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(1)').text().trim(),
							'implement_subject':implement_subject,
							'implement_subject_code':implement_subject_code,
							'implement_subject_nature':implement_subject_nature,
							'exercise_level':exercise_level,
							'power_source':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(0)').text().trim(),
							'joint_agency':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(1)').text().trim(),
							'event_type':event_type,
							'limitation':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(5) td:eq(1)').text().trim(),
							'event_status':event_status,
							'event_version':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(6) td:eq(1)').text().trim(),
							'service_object':service_object,
							'processing_type':processing_type,
							'scope_application':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(1+tiao)+') td:eq(0)').text().trim(),
							'intermediary_services':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(1) td:eq(1)').text().trim(),
							'application_form':application_form,
							'is_entry':is_entry,
							'is_online_office':is_online_office,
							'online_office_depth':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(3+treq)+') td:eq(1)').text().trim(),
							'computer_is_login':computer_is_login,
							'computer_link':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') td:eq(1)').text().trim(),
							'phone_is_login':phone_is_login,
							'phone_link':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') td:eq(1)').text().trim(),
							'is_self_service':is_self_service,
							'is_reservation':is_reservation,
							'is_charge':is_charge,
							'is_online_payment':is_online_payment,
							'is_express_delivery':is_express_delivery,
							'approval_result_type':$('.vue-banshi-jian-x-jiben-x tbody:eq(2) tr:eq(0) td:eq(0)').text().trim(),
							'approval_result_name':$('.vue-banshi-jian-x-jiben-x tbody:eq(2) tr:eq(0) td:eq(1)').text().trim(),
							'intermediary_services_name':intermediary_services_name
						}
				}else{
					data = {
							'unit_id':unit_id,
							'base_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(0)').text().trim(),
							'implement_code':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(1) td:eq(1)').text().trim(),
							'implement_subject':implement_subject,
							'implement_subject_code':implement_subject_code,
							'implement_subject_nature':implement_subject_nature,
							'exercise_level':exercise_level,
							'power_source':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(0)').text().trim(),
							'joint_agency':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(4) td:eq(1)').text().trim(),
							'event_type':event_type,
							'limitation':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(5) td:eq(1)').text().trim(),
							'event_status':event_status,
							'event_version':$('.vue-banshi-jian-x-jiben-x tbody:eq(0) tr:eq(6) td:eq(1)').text().trim(),
							'service_object':service_object,
							'processing_type':processing_type,
							'scope_application':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(1+tiao)+') td:eq(0)').text().trim(),
							'intermediary_services':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(1) td:eq(1)').text().trim(),
							'application_form':application_form,
							'is_entry':is_entry,
							'is_online_office':is_online_office,
							'online_office_depth':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(3+treq)+') td:eq(1)').text().trim(),
							'computer_is_login':computer_is_login,
							'computer_link':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') td:eq(1)').text().trim(),
							'phone_is_login':phone_is_login,
							'phone_link':$('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') td:eq(1)').text().trim(),
							'is_self_service':is_self_service,
							'is_reservation':is_reservation,
							'is_charge':is_charge,
							'is_online_payment':is_online_payment,
							'is_express_delivery':is_express_delivery
						}
				}
			}


			
			await addDb(data,result[i]['id']);

		}
		
		
	}



		
}

//插入数据库
async function addDb(data,id){
	// console.log(data.nameId)
	//插入数据
		let addSql=`update m_matter SET ? where id = '${id}'`;
		let addSqlParams = data;
		return await new Promise(resolve => {
			connection.query(addSql,addSqlParams,function (err, result) {
		        if(err){
		         console.log('[INSERT ERROR] - ',err.message);
		         return;
		        }else{
		        	console.log('UPDATA ID:',id);
			    	return resolve(result.insertId);
		        }        
		 		
			});
		})
	// let sql=`select * from m_matter WHERE id='${id}' AND base_code IS NOT NULL`;
	// let result=await connection.query(sql);
	// if(result.length>0){
	// 	console.log(`add ${id} error`);
	// }else{
		
		
	// }
	
		
}

//插入数据库
async function asySql(addSql,addSqlParams){
	return await new Promise(resolve => {
		connection.query(addSql,addSqlParams,function (err, result) {
	        if(err){
	         console.log('[INSERT ERROR] - ',err.message);
	         return;
	        }else{
	        	console.log('INSERT ID:',result.insertId);
		    	return resolve(result.insertId);
	        }        
	 		
		});
	})
		
}
getList();
